﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MutFormStudio {
	public partial class TabStudio : UserControl, IEnumerable<StudioFormHead> {
		Studio owner;
		public Studio Owner {get=> owner; 
			set {
				owner = value;
				this.HeadFLP.ContextMenuStrip = owner.FixedSizeCSM;
			}
		}

		public TabStudio() {
			InitializeComponent();
		}
		public readonly BaseStudio Base_studio;
		public TabStudio(Studio owner) {
			InitializeComponent();
			Base_studio = owner.Base_studio;
		}
		public ControlCollection Heads => HeadFLP.Controls;
		internal void addControl(Control c, StudioFormHead before = null) {
			var head = new StudioFormHead(c, this);
			HeadFLP.Controls.Add(head);
			if (before != null) {
				HeadFLP.Controls.SetChildIndex(head, HeadFLP.Controls.GetChildIndex(before));
			}
			Controls.Add(c);
			c.Dock = DockStyle.Fill;
			showHeadControl(head);
		}
		internal void removeControl(StudioFormHead sender) {
			Controls.Remove(sender.control);
			HeadFLP.Controls.Remove(sender);
			if (HeadFLP.Controls.Count == 0) {
				showHeadControl(null);
				Owner.OnTabStudioEmpty(this);
			} else {
				int last_select = this.Max(sfh => sfh.Selected_sequence);
				showHeadControl(this.First(sfh => last_select == sfh.Selected_sequence));
			}

		}



		StudioFormHead current_head = null;

		static int Selected_sequence = 0;
		internal void showHeadControl(StudioFormHead sender) {
			if (current_head != null) {
				current_head.Is_selected = false;
			}
			current_head = sender;
			if (current_head != null) {
				current_head.Is_selected = true;
				sender.control.BringToFront();
				sender.Selected_sequence = Selected_sequence++;
			}
		}

		internal void headDragBegin(object sender, MouseEventArgs e) {
			if (MouseButtons == MouseButtons.Left) {
				StudioFormHead from = sender as StudioFormHead;
				foreach (var ss in  Base_studio.studios) {
					ss.switchShowSplitPanel(true);
				}
				DragDropEffects dde = DoDragDrop(from, DragDropEffects.Move);
				foreach (var ss in Base_studio.studios) {
					ss.switchShowSplitPanel(false);
				}
			}
		}
		internal void headDragEnter(object sender, DragEventArgs e) {
			Base_studio.dragEnter(sender, e);
		}

		internal void headDragDrop(object sender, DragEventArgs e) {
			StudioFormHead from = Base_studio.dragEnter(sender, e);
			if (from == null) {
				return;
			}
			if (sender is StudioFormHead) {
				StudioFormHead to = sender as StudioFormHead;
				if (from != to) {
					Control c = from.control;
					from.tag_studio.removeControl(from);
					to.tag_studio.addControl(c, to);
				} else {
					to.MainBTN_Click(sender, e);
				}
			} else {
				Control c = from.control;
				from.tag_studio.removeControl(from);
				addControl(c);
			}
		}
		public IEnumerator<StudioFormHead> GetEnumerator() {
			foreach (var head in HeadFLP.Controls) {
				yield return (StudioFormHead)head;
			}
		}
		IEnumerator IEnumerable.GetEnumerator() {
			foreach (var head in HeadFLP.Controls) {
				System.Diagnostics.Debug.Assert(head is StudioFormHead);
				yield return head;
			}
		}

	}
}

